package zyz.framework.example.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zyz.framework.controller.BaseController;
import zyz.framework.dao.query.QueryItem;
import zyz.framework.dao.query.Where;
import zyz.framework.entity.R;
import zyz.framework.example.entity.UserEntity;

import java.util.Date;
import java.util.List;

/**
 * @author zyz
 * @fileName ExampleController
 * @createTime 2017/12/11  16:04
 * @describe
 * @email zhangyz@ogawatech.com.cn
 */
@RestController
@RequestMapping("api/example")
public class ExampleController extends BaseController {

    /**
     * 插入
     *
     * @return
     */
    @GetMapping("/insert")
    public R insertExample() throws Exception {
        //1. 实体插入
        UserEntity userEntity = new UserEntity();
        userEntity.setUsername("李四");
        userEntity.setCreateTime(new Date());
        userEntity.setEmail("zhangyz@ogawatech.com.cn");
        R r = this.insert(userEntity, "userId");
        return r;
    }

    /**
     * 更新
     *
     * @return
     */
    @GetMapping("/update")
    public R updateExample() throws Exception {
        QueryItem queryItem = new QueryItem();
        queryItem.setWhere(Where.eq("email", "zhangyz@ogawatech.com.cn"));
        UserEntity userEntity = this.getOne(queryItem, UserEntity.class);
        //1. 实体插入
        userEntity.setUsername("李五");
        userEntity.setCreateTime(new Date());
        userEntity.setEmail("zhangyz@ogawatech.com.cn");
        R r = this.update(userEntity, "user_id");
        return r;
    }

    /**
     * 查询
     *
     * @return
     */
    @GetMapping("/queryList")
    public R queryListExample() throws Exception {
        QueryItem queryItem = new QueryItem();
        queryItem.setWhere(Where.eq("email", "zhangyz@ogawatech.com.cn"));
        List<UserEntity> userEntityList = this.getList(queryItem, UserEntity.class);
        R r = new R();
        r.setData(userEntityList);
        return r;
    }

    /**
     * 查询
     *
     * @return
     */
    @GetMapping("/queryOne")
    public R queryOneExample() throws Exception {
        QueryItem queryItem = new QueryItem();
        queryItem.setWhere(Where.eq("user_id", "15"));
        UserEntity userEntityList = this.getOne(queryItem, UserEntity.class);
        R r = new R();
        r.setData(userEntityList);
        return r;
    }

    /**
     * 查询
     *
     * @return
     */
    @GetMapping("/delete")
    public R deleteExample() throws Exception {

        return this.deleteById("user_id","15", UserEntity.class);
    }

}
